package io.gitee.yth.design.pattern.test.core.pattern.behavioral.observer;

import java.util.Calendar;

/**
 * @author yth
 * @since 2023-04-30-15:08
 */
public class ClockTimer extends AbstractSubject {

    private int hour;
    private int minute;
    private int second;

    public ClockTimer() {
        final Calendar calendar = Calendar.getInstance();
        this.hour = calendar.get(Calendar.HOUR_OF_DAY);
        this.minute = calendar.get(Calendar.MINUTE);
        this.second = calendar.get(Calendar.SECOND);
    }

    /**
     * 内部状态修改时，调用notifyObservers()接口方法
     */
    public void tick() {
        if (++second == 60) {
            second = 0;
            if (++minute == 60) {
                minute = 0;
                if (++hour == 24) {
                    hour = 0;
                }
            }
        }
        notifyObservers();  // message feed
    }

    @Override
    public String toString() {
        return "ClockTimer{" +
                "hour=" + hour +
                ", minute=" + minute +
                ", second=" + second +
                '}';
    }

    public int getHour() {
        return hour;
    }

    public int getMinute() {
        return minute;
    }

    public int getSecond() {
        return second;
    }

}
